package org.spi;

import java.util.regex.Pattern;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.ArrayList;
import java.util.List;

public class Douban_spider {

    List<Paper> papers_curr = new ArrayList<>();
    public Douban_spider() {

    }

    // 检查字符串是否包含不支持的字符
    private boolean containsUnsupportedCharacters(String input) {
        // 这里可以使用正则表达式检查不支持的字符
        // 例如，检查是否包含 emoji（可以根据需要调整）
        String regex = "[^\\u0000-\\uFFFF]"; // 查找不在基本多语言平面的字符
        return Pattern.compile(regex).matcher(input).find();
    }

    public List<Paper> Db(int numOfPages, WebDriver driver) {

        // 导航到豆瓣网站
        driver.get("https://movie.douban.com/review/best/");

        // 每一页的影评数
        int numOfCommments = 10;
        // 影评爬取
        for (int page = 0; page < numOfPages; page++) {


            for (int commments = 1; commments <= numOfCommments; commments++) {
                // 歇一会儿

                // xpath路径
                // String cur_path = "//*[@id=\"content\"]/div/div[1]/div[1]/div[" + commments + "]";
                String cur_path = "/html/body/div[3]/div[1]/div/div[1]/div[1]/div["+ commments + "]";

                List<WebElement> elements = driver.findElements(By.xpath(cur_path));
                // 开始逐个小框寻找
                for (WebElement element : elements) {
                    Paper paper_curr = new Paper();
                    // 标题
                    WebElement title_element = element.findElement(By.tagName("h2"));
                    String title = title_element.getText();
                    paper_curr.setTitle(title);

                    // 链接
                    String link = title_element.findElement(By.tagName("a")).getAttribute("href");
                    paper_curr.setLink(link);

                    // 时间
                    WebElement time_element = element.findElement(By.className("main-meta"));
                    paper_curr.setDate(time_element.getText());

                    // 作者
                    WebElement author_element = element.findElement(By.className("name"));
                    String author_String = author_element.getText();
                    // 检查 author 字符串是否包含不支持的字符
                    if (containsUnsupportedCharacters(author_String)) {
                        System.out.println("Unsupported characters found in author: " + author_String);
                        // 可以选择设置为默认值或跳过
                        paper_curr.setAuthor("Unknown Author");
                    } else {
                        paper_curr.setAuthor(author_element.getText());
                    }


                    // 部分评价
                    WebElement abelement = element.findElement(By.className("short-content"));
                    paper_curr.setSummary(abelement.getText());

                    paper_curr.setType("Movie");

                    System.out.println("paper_curr: " + paper_curr.getTitle());
                    System.out.println("Author: " + paper_curr.getAuthor() + "\n");
                    // 更新paper列表
                    papers_curr.add(paper_curr);
                }

            }
        }
        // 退出浏览器
        driver.quit();
        System.out.println("\nSpider end~");
        return papers_curr;
    }

}
